package Leetcode.DayTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName Test_426
 * @since: 2024/12/1 10:28
 * @auth: kirito
 * @description: 周赛426
 **/
public class Test_426 {
    public static void main(String[] args) {
        Test_426 test = new Test_426();


        int[] arr = {-642,-734,-213,-521};
        System.out.println(test.getLargestOutlier(arr));

    }
    public int getLargestOutlier(int[] nums) {
        int ans = Integer.MIN_VALUE;
        int n = nums.length;

        int sum = 0;
        Arrays.sort(nums);
        for (int num : nums) {
            sum += num;
        }

        for (int i = 0; i < n; i++) {
            int target = sum - nums[i];
            int left = 0;
            int right = n-1;
            while (left <= right) {
                int mid = left + (right - left) / 2;
                int temp = target - nums[mid];
                if (mid == i) {
                    left = mid + 1; // 跳过当前元素
                } else if (nums[mid] == temp) {
                    ans = Math.max(ans, nums[i]);
                    break; // 找到目标值，更新答案并退出循环
                } else if (nums[i] == temp) {
                    ans = Math.max(ans, nums[mid]);
                    break; // 找到目标值，更新答案并退出循环
                } else if (nums[mid] < temp) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
        }
        return ans;
    }
    public int[] maxTargetNodes(int[][] edges1, int[][] edges2, int k) {
        return null;
    }

}
